/****************************************************************************
 Copyright (c) 2012-2012 lijiejun
 Copyright (c) 2010-2012 cocos2d-x.org
 Copyright (c) 2008-2010 Ricardo Quesada
 Copyright (c) 2011      Zynga Inc.
 
 http://www.cocos2d-x.org
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

package org.cocos2d.loaders.misc.parsers
{
	import flash.display.BitmapData;
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	
	import org.cocos2d.events.AssetEvent;
	import org.cocos2d.events.ParserEvent;
	import org.cocos2d.loaders.misc.parsers.utils.ParserUtil;
	
	public class ParserBase extends EventDispatcher
	{
		public var _fileName : String;
		protected var _dataFormat : String;
		protected var _data : *;
		protected var _frameLimit : Number;
		protected var _lastFrameTime : Number;
		
		protected function getByteData() : ByteArray
		{
			return ParserUtil.toByteArray(_data);
		}
		
		protected static const PARSING_DONE : Boolean = true;
		
		private var _parsingComplete : Boolean;
		private var _parsingFailure : Boolean;
		private var _timer : Timer;
		
		public function ParserBase(format : String)
		{
			_dataFormat = format;
		}
		
		public function set parsingFailture(b : Boolean) : void
		{
			_parsingFailure = b;
		}
		
		public function get parsingFailture() : Boolean
		{
			return _parsingFailure;
		}
		
		public function get dataFormat() : String
		{
			return _dataFormat;
		}
		
		public function parseAsync(data : *, frameLimit : Number = 30) : void
		{
			_data = data;
			
			startParsing(frameLimit);
		}
		
		protected function finalizeAsset(asset : *, name : String = null) : void
		{
			var type_event : String;
			var type_name : String;
			
			if (asset is BitmapData)
			{
				type_name = 'texture';
				type_event = AssetEvent.BITMAP_COMPLETE;
			}
			
			dispatchEvent(new AssetEvent(type_event, asset));
		}
		
		protected function proceedParsing() : Boolean
		{
			throw new Error("abstract method called");
			return true;
		}
		
		protected function onInterval(event : TimerEvent = null) : void
		{
			_lastFrameTime = getTimer();
			
			if (proceedParsing() && !_parsingFailure)
				finishParsing();
		}
		
		private function startParsing(frameLimit : Number) : void
		{
			_frameLimit = frameLimit;
			_timer = new Timer(_frameLimit, 0);
			_timer.addEventListener(TimerEvent.TIMER, onInterval);
			_timer.start();
		}
		
		protected function finishParsing() : void
		{
			_timer.removeEventListener(TimerEvent.TIMER, onInterval);
			_timer.stop();
			_timer = null;
			_parsingComplete = true;
			dispatchEvent(new ParserEvent(ParserEvent.PARSE_COMPLETE));
		}
	}
}